<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            
            <el-table-column prop="order_no" label="订单号" width="200"/>
            <!-- <el-table-column prop="user_id" label="用户ID" width="100"/> -->
            <el-table-column prop="address_nickname" label="收货人" width="100"/>
            <el-table-column prop="address_phone" label="联系电话" width="120"/>
            <el-table-column prop="address_detail" label="收货地址" min-width="200">
              <template slot-scope="scope">
                <div class="address-content">
                  {{ scope.row.address_detail }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="status" label="订单状态" width="120">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 'pending'" type="warning">待支付</el-tag>
                <el-tag v-else-if="scope.row.status === 'paid'" type="primary">已支付</el-tag>
                <el-tag v-else-if="scope.row.status === 'refunding'" type="warning">退款中</el-tag>
                <el-tag v-else-if="scope.row.status === 'shipped'" type="success">待收货</el-tag>
                <el-tag v-else-if="scope.row.status === 'refunded'" type="danger">已退款</el-tag>
                <el-tag v-else-if="scope.row.status === 'completed'" type="success">已完成</el-tag>
                <el-tag v-else-if="scope.row.status === 'cancelled'" type="info">已取消</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="order_delivery" label="配送方式" width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.order_delivery === 'express'" type="primary">快递</el-tag>
                <el-tag v-else-if="scope.row.order_delivery === 'self_pickup'" type="warning">自提</el-tag>
                <el-tag v-else-if="scope.row.order_delivery === 'no_delivery'" type="info">虚拟商品</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="count" label="商品数量" width="100"/>
            <el-table-column prop="amount" label="订单金额" width="120">
              <template slot-scope="scope">
                ¥{{ scope.row.amount }}
              </template>
            </el-table-column>
            <el-table-column prop="payment_amount" label="支付金额" width="120">
              <template slot-scope="scope">
                ¥{{ scope.row.payment_amount }}
              </template>
            </el-table-column>
            <!-- <el-table-column prop="payment_type" label="支付方式" width="100"/> -->
            <!-- <el-table-column prop="refund_status" label="退款状态" width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.refund_status === -1" type="info">未退款</el-tag>
                <el-tag v-else-if="scope.row.refund_status === 0" type="warning">退款中</el-tag>
                <el-tag v-else-if="scope.row.refund_status === 1" type="success">退款成功</el-tag>
                <el-tag v-else-if="scope.row.refund_status === 2" type="danger">退款失败</el-tag>
              </template>
            </el-table-column> -->
            <el-table-column prop="create_time" label="创建时间" width="160">
              <template slot-scope="scope">
                {{ formatDateTime(scope.row.create_time) }}
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="250">
              <template slot-scope="scope">
                <el-button type="text" @click="onDetail(scope.row)">详情</el-button>
                <el-button type="text" v-if="scope.row.status === 'paid'" @click="onShip(scope.row)">发货</el-button>
                <el-button type="text" v-if="scope.row.status === 'shipped'" @click="onShip(scope.row)">修改快递</el-button>
                <el-button type="text" v-if="scope.row.status === 'paid' || scope.row.status === 'shipped' || scope.row.status === 'completed'" style="color: #F56C6C" @click="onRefund(scope.row)">退款</el-button>
                <el-button type="text" v-if="scope.row.status === 'pending' || scope.row.status === 'cancelled'" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
    <Detail ref="detailFormRef" />
    <Ship ref="shipFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
import Detail from "./detail";
import Ship from "./ship";

export default {
  name: "OrderIndex",
  components: {
    Add,
    Detail,
    Ship
  },
  mixins: [crud],
  data() {
    return {
      request: http.order,
      searchList: [
        {
          label: '订单号',
          prop: 'order_no',
          name: 'input',
          placeholder: '请输入订单号'
        },
        {
          label: '用户ID',
          prop: 'user_id',
          name: 'input',
          placeholder: '请输入用户ID'
        },
        {
          label: '订单状态',
          prop: 'status',
          name: 'select',
          placeholder: '请选择订单状态',
          options: [
            // { value: 'pending_pricing', label: '待定价' },
            { value: 'pending', label: '待支付' },
            { value: 'paid', label: '已支付' },
            { value: 'refunding', label: '退款中' },
            { value: 'shipped', label: '待收货' },
            { value: 'refunded', label: '已退款' },
            { value: 'completed', label: '已完成' },
            { value: 'cancelled', label: '已取消' },
          ]
        },
        // {
        //   label: '配送方式',
        //   prop: 'order_delivery',
        //   name: 'select',
        //   placeholder: '请选择配送方式',
        //   options: [
        //     { value: 'express', label: '快递' },
        //     { value: 'self_pickup', label: '自提' },
        //     { value: 'no_delivery', label: '虚拟商品' }
        //   ]
        // }
      ],
      searchParams: {
        order_no: '',
        user_id: '',
        status: '',
        order_delivery: ''
      },
      defaultParams: {
      },
      tableData: []
    }
  },
  methods: {
    formatDateTime(dateTime) {
      if (!dateTime) return '';
      return new Date(dateTime).toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      });
    },
    onDetail(row) {
      this.$refs.detailFormRef.open(row);
    },
    onShip(row) {
      this.$refs.shipFormRef.open(row);
    },
    onRefund(row) {
      this.$confirm('确认申请退款？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'info',
          message: '退款申请中...'
        });
        
        // 调用退款接口
        this.request.createOrderRefund({ 
          id: row.id, 
          refund_reason: '管理员申请退款',
          is_admin: 1
        }).then(res => {
          this.$message({
            type: 'success',
            message: '退款申请成功'
          });
          
          // 自动确认退款
          this.request.confirmOrderRefund({
            id: row.id,
            refund_remark: '管理员申请退款',
            refund_amount: row.payment_amount,
            approve: true,
            is_admin: 1
          }).then(res => {
            this.$message({
              type: 'success',
              message: '退款处理成功'
            });
            // 刷新页面数据
            this.initData();
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退款'
        });
      });
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
.address-content {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style> 